<template>
  <el-menu
    :collapse="isCollapse"
    :default-active="$route.path"
    class="el-menu-vertical-demo"
    background-color="#545c64"
    text-color="#fff"
    active-text-color="#ffd04b"
  >
    <h3 v-show="isCollapse">系统</h3>
    <h3 v-show="!isCollapse">贝优商城管理系统</h3>
    <el-menu-item
      :index="item.path"
      v-for="item in noChildren"
      :key="item.path"
      @click="clickMenu(item)"
    >
      <i :class="'el-icon-' + item.icon"></i>
      <span slot="title">{{ item.label }}</span>
    </el-menu-item>
    <el-submenu
      :index="item.label"
      v-for="(item, index) in hasChildren"
      :key="index"
    >
      <template slot="title">
        <i :class="'el-icon-' + item.icon"></i>
        <span slot="title">{{ item.label }}</span>
      </template>
      <el-menu-item-group>
        <el-menu-item
          :index="subItem.path"
          v-for="(subItem, subIndex) in item.children"
          :key="subIndex"
          @click="clickMenu(subItem)"
        >
          <i :class="'el-icon-' + subItem.icon"></i>
          <span slot="title">{{ subItem.label }}</span>
        </el-menu-item>
      </el-menu-item-group>
    </el-submenu>
  </el-menu>
</template>

<script>
export default {
  computed: {
    noChildren() {
      return this.asideMenu.filter((item) => !item.children);
    },
    hasChildren() {
      return this.asideMenu.filter((item) => item.children);
    },
    isCollapse() {
      return this.$store.state.tab.isCollapse;
    },
    menu() {
      return this.$store.state.tab.menu;
    },
  },
  created() {
    this.getmenus();
  },
  data() {
    return {
      asideMenu: [
        // {
        //   path: "/",
        //   name: "home",
        //   label: "首页",
        //   icon: "s-home",
        // },
        // {
        //   path: "/video",
        //   name: "video",
        //   label: "商品管理",
        //   icon: "video-play",
        // },
        // {
        //   path: "/user",
        //   name: "user",
        //   label: "用户管理",
        //   icon: "user",
        // },
        // {
        //   label: "其他",
        //   icon: "user",
        //   children: [
        //     {
        //       path: "/page1",
        //       name: "page1",
        //       label: "页面1",
        //       icon: "setting",
        //     },
        //     {
        //       path: "/page2",
        //       name: "page2",
        //       label: "页面2",
        //       icon: "setting",
        //     },
        //   ],
        // },
      ],
    };
  },
  methods: {
    // 获取左侧菜单列表
    getmenus() {
      // const { data: res } = await this.$http.get('menus')
      // if (res.meta.status !== 200) return this.$message.error('获取左侧菜单列表失败！')
      // // 把获取到的菜单数据，保存到 data 的 menus 数组中，供页面使用
      // this.menus = res.data
      let loginType = window.sessionStorage.getItem("loginType");
      if (loginType == 1) {
        let data = [
          {
            label: "商品管理",
            icon: "user",
            children: [
              {
                path: "/goods/list",
                name: "goods/list",
                label: "商品列表",
                icon: "user",
              },
              {
                path: "/goods/add",
                name: "goods/add",
                label: "添加商品",
                icon: "user",
              },
            ],
          },
          {
            label: "订单管理",
            icon: "user",
            children: [
              {
                path: "/cateList",
                name: "cateList",
                label: "订单列表",
                icon: "user",
              },
            ],
          },
        ];
        this.asideMenu = data;
      } else {
        this.$http.post("/manage/admin/getRoleRulesById", {}).then((res) => {
          //console.log(res)
          if (res.data.status == 1) {
            res.data.data.forEach((oneItem) => {
              oneItem.label = oneItem.name;
              oneItem.icon = "user";
              let name = oneItem.path.slice(1);
              oneItem.name = name;
              oneItem.children.forEach((twoItem) => {
                twoItem.label = twoItem.name;
                twoItem.icon = "user";
                let name1 = twoItem.path.slice(1);
                twoItem.name = name1;
              });
            });
            this.asideMenu = res.data.data;
            console.log(this.asideMenu);
          } else {
          }
        });
      }
    },
    clickMenu(item) {
      console.log(item.name);
      this.$router.push({ name: item.name });
      this.$store.commit("selectMenu", item);
    },
  },
};
</script>

<style lang="less" scoped>
.el-menu {
  height: 100%;
  border: none;
  h3 {
    color: #ffffff;
    text-align: center;
    line-height: 48px;
  }
}
.el-menu-vertical-demo:not(.el-menu--collapse) {
  width: 200px;
  min-height: 400px;
}
</style>
